package com.itheima.health.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.itheima.health.entity.PageResult;
import com.itheima.health.entity.QueryPageBean;
import com.itheima.health.mapper.AppoMapper;
import com.itheima.health.pojo.CheckItem;
import com.itheima.health.service.CheckItemService;
import org.apache.commons.lang.StringUtils;
import org.apache.dubbo.config.annotation.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

//import com.itheima.health.pojo.User22;

/**
 * @program: health_paret
 * @description:
 * @author: Mr.Wang
 * @create: 2020-11-03 20:20
 **/
@Service
@Transactional
public class CheckItemServiceImpl extends ServiceImpl<AppoMapper, CheckItem> implements CheckItemService {
    //分页查询
    @Override
    public PageResult findPage(QueryPageBean queryPageBean) {
//select * from t_user where setIs_delete = 0
        // 查看是否删除把0和1作为参数传进去
        QueryWrapper<CheckItem> wrapper = new QueryWrapper<CheckItem>();
        wrapper.eq("Is_delete", 0);
        //这里进行判断是有条件查询还是无条件查询的
        Page<CheckItem> page = null;
        if (!StringUtils.isBlank(queryPageBean.getQueryString())) {
            String queryString = queryPageBean.getQueryString();
            wrapper.like("name", queryString).or().like("code", queryString);
            page = this.page(new Page<CheckItem>(queryPageBean.getCurrentPage(), queryPageBean.getPageSize()), wrapper);
        } else {
            page = this.page(new Page<CheckItem>(queryPageBean.getCurrentPage(), queryPageBean.getPageSize()), wrapper);
        }

        List<CheckItem> records = page.getRecords();
        long total = page.getTotal();
        PageResult result = new PageResult(total, records);
        return result;
    }

    // 进行逻辑上的删除
    @Override
    public Boolean delete(int id) {
        CheckItem checkItem = new CheckItem();
        checkItem.setId(id);
        checkItem.setIs_delete(1);
        boolean b = this.updateById(checkItem);
        System.out.println("b = " + b);
        return b;
    }

    //进行添加检查项以及修改检查项
    @Override
    public Boolean add(CheckItem checkItem) {
        checkItem.setIs_delete(0);
        //这个方法的话就是把增加和修改给整合了
        boolean b = this.saveOrUpdate(checkItem);
        return b;
    }

}
